package net.caixiaomi.info.Lottery.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import net.caixiaomi.info.Lottery.callback.LotteryMainCallBack;
import net.caixiaomi.info.Lottery.model.LotteryTicketInfo;
import net.caixiaomi.info.Lottery.presenter.BallPresenter;
import net.caixiaomi.info.Lottery.ui.BallLotteryMainActivity;
import net.caixiaomi.info.Lottery.ui.HistoryPopupWindow;
import net.caixiaomi.info.Lottery.view.BallView;
import net.caixiaomi.info.R;


/**
 * Created by suntian on 2018/8/6.
 */

public class StandardSelectFragment extends Fragment implements View.OnClickListener{

    private BallView ballViewBefore;
    private BallView ballViewAfter;
    private TextView mDateTv;
    private TextView mPrizes;

    private BallPresenter mPresenter;
    private LotteryMainCallBack mMainCallBack;

    private HistoryPopupWindow mPopupWindow;
    private boolean mControl;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view=View.inflate(getActivity(), R.layout.standard_layout,null);
        ballViewBefore= view.findViewById(R.id.ballview1);
        ballViewAfter= view.findViewById(R.id.ballview3);
        mDateTv=view.findViewById(R.id.lottery_date_des);
        mPrizes=view.findViewById(R.id.lottery_history_prizes);
        mPresenter=new BallPresenter(getActivity());
        ballViewBefore.init(getResources().getColor(R.color.white), getResources().getColor(R.color.red), getResources().getColor(R.color.red), getResources().getColor(R.color.white), BallLotteryMainActivity.BALL_BEFORE, mPresenter);
        ballViewAfter.init(getResources().getColor(R.color.white), getResources().getColor(R.color.blue), getResources().getColor(R.color.blue), getResources().getColor(R.color.white),BallLotteryMainActivity.BALL_AFTER, mPresenter);
        mPresenter.initStandardView(ballViewBefore,ballViewAfter,0,mMainCallBack);
        view.findViewById(R.id.lottery_history_btn).setOnClickListener(this);
        view.findViewById(R.id.lottery_shock_btn).setOnClickListener(this);

        mPopupWindow=new HistoryPopupWindow();
        mPopupWindow.init(getActivity());

        ballViewBefore.setMissSwitch(mControl);
        ballViewAfter.setMissSwitch(mControl);
        return view;
    }

    public void setCallBack(LotteryMainCallBack callBack){
        mMainCallBack=callBack;
    }

    public void setNumMissControl(boolean control){
        mControl=control;
        if(ballViewBefore!=null) {
            ballViewBefore.setMissSwitch(control);
            ballViewAfter.setMissSwitch(control);
        }
    }

    public void setShock(){
        mPresenter.setShock();  //摇一摇选号
    }

    public void clearBall(){
        ballViewBefore.setCheckData(null,true);
        ballViewAfter.setCheckData(null,true);
    }

    public void setInfo(LotteryTicketInfo info){
        mDateTv.setText(info.getTerm_num()+"期 截止时间 "+info.getEndDate());
        mPrizes.setText(info.getPrizes());

        mPopupWindow.setData(info.getPrizeList());
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.lottery_history_btn:
                mPopupWindow.showHistoryPopupWindow(mDateTv);
            break;
            case R.id.lottery_shock_btn:
                mPresenter.setShock();  //摇一摇选号
                break;
        }
    }

    public void addBetingData(){
        mPresenter.addBettingData();
    }

    public boolean isNoBallCheck(){
        return mPresenter.isNoBallCheck();
    }
}
